package myauctioneer.controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import myauctioneer.App;
import myauctioneer.dto.Rating;
import myauctioneer.dto.RatingDetail;
import myauctioneer.dto.User;
import myauctioneer.service.UserService;

/**
 * Servlet implementation class UserServlet
 */
@javax.servlet.annotation.WebServlet("/user/*")
public class UserServlet extends DefaultServlet {
	private static final long serialVersionUID = 1L;

	public void registerAction(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		String name, address, phone, email, password;

		name = request.getParameter("name");
		address = request.getParameter("address");
		phone = request.getParameter("phone");
		email = request.getParameter("email");
		password = request.getParameter("password");

		boolean invalid = (name == null || name.trim() == "")
				|| (address == null || address.trim() == "")
				|| (phone == null || phone.trim() == "")
				|| (email == null || email.trim() == "")
				|| (password == null || password.trim() == "");

		if (!invalid) {
			UserService userService = new UserService();

			if (userService.register(name, address, phone, email, password)) {
				request.setAttribute("title", "Registration successfull");
				request.setAttribute("content",
						"The registration was successfull.");
				request.setAttribute("template", "success.jsp");
				return;
			}
		}

		request.setAttribute("template", "register.jsp");
	}

	public void loginAction(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		String email, password;

		email = request.getParameter("email");
		password = request.getParameter("password");

		UserService userService = new UserService();

		if (userService.authenticate(email, password)) {
			User user = userService.getUserDetails(email);
			request.getSession().setAttribute("user", user);
			request.setAttribute("title", "Login successfull");
			request.setAttribute("content", "The login was successfull.");
			request.setAttribute("template", "success.jsp");
		} else {
			response.sendRedirect(App.getBaseUrl());
		}
	}

	public void logoutAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getSession().invalidate();
		request.setAttribute("title", "Logout successfull");
		request.setAttribute("content", "The Logout was successfull.");
		request.setAttribute("template", "success.jsp");
	}

	public void profileAction(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		int userId = Integer.parseInt(request.getParameter("id"));
		User user = (new myauctioneer.dao.UserDAO()).loadUserByUserId(userId);
		request.setAttribute("user", user);
		request.setAttribute(
				"numberArticleRatings",
				(new myauctioneer.dao.RatingDAO()).loadRatingsByUserIdAndType(
						userId, "articleRating").size());
		request.setAttribute(
				"numberSellerRatings",
				(new myauctioneer.dao.RatingDAO()).loadRatingsByUserIdAndType(
						userId, "sellerRating").size());
		request.setAttribute(
				"numberBuyerRatings",
				(new myauctioneer.dao.RatingDAO()).loadRatingsByBuyerId(
//old						user.getUserId()).size());
						user.getUserId(), "buyerRating").size()); //simon, neu
		request.setAttribute("template", "profile.jsp");
	}

	public void showRatingDetailsAction(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		int userId = Integer.parseInt(request.getParameter("userId"));
		String type = request.getParameter("type");

		ArrayList<Rating> rAL = new ArrayList<Rating>();
		ArrayList<RatingDetail> rDAL = new ArrayList<RatingDetail>();

		if (type.equals("buyerRating")) {
			rAL = (new myauctioneer.dao.RatingDAO())
//old					.loadRatingsByBuyerId(userId);
					.loadRatingsByBuyerId(userId, type); //simon, neu
		} else {
			rAL = (new myauctioneer.dao.RatingDAO())
					.loadRatingsByUserIdAndType(userId, type);
		}

		for (Rating r : rAL) {
			rDAL.add((new myauctioneer.dao.RatingDetailDAO())
					.loadRatingDetailByRatingDetailId(r.getRatingDetailId()));
		}

		request.setAttribute("userId", userId);
		request.setAttribute("type", type);
		request.setAttribute("rAL", rAL);
		request.setAttribute("rDAL", rDAL);
		request.setAttribute("template", "showRatingDetails.jsp");
	}
}
